package com.ssm.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ssm.pojo.User;

@Controller
@RequestMapping("/common/rest2")
public class RestController2 {
	private static final Logger logger = LoggerFactory.getLogger(RestController2.class);
	
	
	@RequestMapping(value = "/showUser", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object toIndex(HttpServletRequest request, @RequestParam(name="id",required=true) String id, HttpServletResponse response) {
		logger.info("==========id========");
		logger.info(id);

		if (null != id) {
			int userId = Integer.parseInt(id);
			User user = new User();
			user.setId(userId);
			user.setUserName("wxg");
			user.setPassword("123456");
			user.setAge(32);
			return buildOpenapiResponse(user, "用户信息！");
		} else {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "1");
			map.put("errorMsg", "出错，请联系管理员！");
			return buildOpenapiResponse(map, "出现异常！");
		}
	}
	
	@RequestMapping(value = "/showUserJson", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object toIndexJson(HttpServletRequest request, @RequestBody String json, HttpServletResponse response) {
		logger.info("==========json========");
		logger.info(json);
		JSONObject obj = JSON.parseObject(json);
		String id = obj.getString("id");

		if (null != id) {
			int userId = Integer.parseInt(id);
			User user = new User();
			user.setId(userId);
			user.setUserName("wxg");
			user.setPassword("123456");
			user.setAge(32);
			return buildOpenapiResponse(user, "用户信息！");
		} else {
			Map<String, String> map = new HashMap<String, String>();
			map.put("errorCode", "1");
			map.put("errorMsg", "出错，请联系管理员！");
			return buildOpenapiResponse(map, "出现异常！");
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/showUserJsonOpenapi2", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object toIndexJsonOpenapi2(HttpServletRequest request, @RequestBody String json, HttpServletResponse response) {
		logger.info("=============json============");
		logger.info(json);
		JSONObject obj = JSON.parseObject(json);
		String id = null;
		
//		System.out.println("---");
//		if(JSONPath.contains(obj, "$.REQUEST.REQUEST_DATA")){
//			@SuppressWarnings("unchecked")
//			Map<String, Object> param = (Map<String, Object>)JSONPath.eval(obj, "$.REQUEST.REQUEST_DATA");
//			if(null != param.get("id")){
//				System.out.println( param.get("id").toString() );
//			}
//		}
//		System.out.println("---");
		
		logger.info("==============");
		if(obj.containsKey("REQUEST")){
			Map<String, Object> map = (Map<String, Object>) obj.get("REQUEST");
			logger.info(map.toString());
			Map<String, Object> data = (Map<String, Object>)map.get("REQUEST_DATA");
			logger.info(data.toString());
			if(null != data.get("id")){
				id = data.get("id").toString();
				logger.info(id);
			}
		}
		if (null != id) {
			int userId = Integer.parseInt(id);
			User user = new User();
			user.setId(userId);
			user.setUserName("wxg");
			user.setPassword("123456");
			user.setAge(32);
			return buildOpenapiResponse(user,"用户信息！");
		} else {
			Map<String,String> map = new HashMap<String,String>();
			map.put("errorCode", "1");
			map.put("errorMsg", "出错，请联系管理员！");
			return buildOpenapiResponse(map,"出现异常！");
		}
	}
	
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
	
	public Map<String,Object> buildOpenapiResponse(Object obj, String desc){
		Map<String,Object> retmap = new HashMap<String,Object>();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("RETURN_CODE", "S000A000");
		map.put("RETURN_STAMP", SDF.format(new java.util.Date()));
		map.put("RETURN_DATA", obj);
		map.put("RETURN_DESC", desc);
		retmap.put("RESPONSE", map);
		return retmap;
	}
}
